﻿using System.Diagnostics;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using Color = System.Windows.Media.Color;
using ColorConverter = System.Windows.Media.ColorConverter;

namespace EC3_6CLH.Model
{
    internal class TestResultConverter : IValueConverter
    {
        public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
        {
            Debug.Assert(value != null, nameof(value) + " != null");
            Debug.Assert(parameter != null, nameof(parameter) + " != null");

            var param = parameter.ToString();
            if (param == "txt")
            {
                var state = (WorkstationState)value;
                var stateMessage = state switch
                {
                    WorkstationState.Idle => "空闲",
                    WorkstationState.Initializing => "初始化中",
                    WorkstationState.Running => "运行中",
                    WorkstationState.Error => "发生错误",
                    WorkstationState.Stop => "停止",
                    WorkstationState.Ok => "测试通过",
                    WorkstationState.Ng => "测试失败",
                    _ => throw new ArgumentOutOfRangeException(nameof(state), state, "未知工位状态")
                };
                return stateMessage;
            }
            else if (param == "color")
            {
                var state = (WorkstationState)value;
                Color color = state switch
                {
                    WorkstationState.Idle => (Color)ColorConverter.ConvertFromString("#A0A0A0"),
                    WorkstationState.Initializing => (Color)ColorConverter.ConvertFromString("#FFA500"),
                    WorkstationState.Running => (Color)ColorConverter.ConvertFromString("#00BFFF"),
                    WorkstationState.Error => (Color)ColorConverter.ConvertFromString("#FF4500"),
                    WorkstationState.Stop => (Color)ColorConverter.ConvertFromString("#A0A0A0"),
                    WorkstationState.Ok => (Color)ColorConverter.ConvertFromString("#32CD32"),
                    WorkstationState.Ng => (Color)ColorConverter.ConvertFromString("#FF6347"),
                    _ => (Color)ColorConverter.ConvertFromString("#000000"),
                };

                return new SolidColorBrush(color);
            }
            else if (param == "StartEnable")
            {
                var state = (WorkstationState)value;
                bool enable = state switch
                {
                    WorkstationState.Idle => true,
                    WorkstationState.Initializing => false,
                    WorkstationState.Running => false,
                    WorkstationState.Error => false,
                    WorkstationState.Stop => true,
                    WorkstationState.Ok => true,
                    WorkstationState.Ng => true,
                    _ => false,
                };
                return enable;
            }
            else if (param == "StopEnable")
            {
                var state = (WorkstationState)value;
                bool enable = state switch
                {
                    WorkstationState.Idle => false,
                    WorkstationState.Initializing => true,
                    WorkstationState.Running => true,
                    WorkstationState.Error => false,
                    WorkstationState.Stop => false,
                    WorkstationState.Ok => false,
                    WorkstationState.Ng => false,
                    _ => false,
                };
                return enable;
            }

            return null;
        }

        public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => null;
    }
}